package archive

import (
	"archive/tar"
	"io"
	"io/fs"

	"github.com/krolaw/zipstream"
)

type entry struct {
	Path string
	fs.FileInfo
	io.Reader
}

type readNext func() (*entry, error)

func readTar(r io.Reader) readNext {
	er := tar.NewReader(r)
	return func() (*entry, error) {
		if h, err := er.Next(); err != nil {
			return nil, err
		} else {
			return &entry{Path: h.Name, FileInfo: h.FileInfo(), Reader: er}, nil
		}
	}
}

func readZip(r io.Reader) readNext {
	er := zipstream.NewReader(r)
	return func() (*entry, error) {
		if h, err := er.Next(); err != nil {
			return nil, err
		} else {
			return &entry{Path: h.Name, FileInfo: h.FileInfo(), Reader: er}, nil
		}
	}
}
